package end.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import end.bean.Album;
import end.bean.Article;
import end.bean.Banner;
import end.bean.Chapter;
import end.bean.CounterGroup;
import end.bean.Customer;
import end.bean.ass.InterAlbum;
import end.bean.ass.InterArticle;
import end.ser.AlbumSER;
import end.ser.ArticleSER;
import end.ser.BannerSER;
import end.ser.ChapterSER;
import end.ser.CounterGroupSER;
import end.ser.CustomerSER;

/**
 * 
 * InteractiveForAndriod.java 2016-7-4
 *
 * Copyright zhaocj  Inc. All rights reserved.
 * Love Me Like Love Justin Bieber
 */
@Controller
public class InteractiveForAndriod {
	@Autowired
	private CounterGroupSER<CounterGroup> counterGroupSER;
	public CounterGroupSER<CounterGroup> getCounterGroupSER() {
		return counterGroupSER;
	}
	public void setCounterGroupSER(CounterGroupSER<CounterGroup> counterGroupSER) {
		this.counterGroupSER = counterGroupSER;
	} 
	@Autowired
	private AlbumSER<Album> albumSER;
    public AlbumSER<Album> getAlbumSER() {
		return albumSER;
	}
    public void setAlbumSER(AlbumSER<Album> albumSER) {
		this.albumSER = albumSER;
	}
    @Autowired
    private ArticleSER<Article> articleSER; 
    public ArticleSER<Article> getArticleSER() {
		return articleSER;
	}
	public void setArticleSER(ArticleSER<Article> articleSER) {
		this.articleSER = articleSER;
	}
	@Autowired
	private BannerSER<Banner> bannerSER;
	public BannerSER<Banner> getBannerSER() {
		return bannerSER;
	}
	public void setBannerSER(BannerSER<Banner> bannerSER) {
		this.bannerSER = bannerSER;
	}
	@Autowired
	private CustomerSER<Customer> customerSER;
	public CustomerSER<Customer> getCustomerSER() {
		return customerSER;
	}
	public void setCustomerSER(CustomerSER<Customer> customerSER) {
		this.customerSER = customerSER;
	}
	@Autowired
	private ChapterSER<Chapter> chapterSER;
	public ChapterSER<Chapter> getChapterSER() {
		return chapterSER;
	}
	public void setChapterSER(ChapterSER<Chapter> chapterSER) {
		this.chapterSER = chapterSER;
	}
	
	@RequestMapping("/first_page")
    public @ResponseBody Map<String,Object> fistPage(Integer uid, String type,
			String sub_type) throws Exception{
		
		Album album = new Album();
		album.getPageUtil().setPaging(true);
		album.getPageUtil().setOrderByCondition("score");
		album.getPageUtil().setQueryCondition("DESC");
		album.getPageUtil().setPageOffset(0);
		album.getPageUtil().setPageSize(6);
		album.setUseStatus('0');
		
	    List<Album> beansAlbum = albumSER.selectByBean(album);
	    List<Object> beansInter = new ArrayList<Object>();
	    for (Album album2 : beansAlbum) {
	    	InterAlbum interAlbum = new InterAlbum();
	    	interAlbum.setAlbum(album2);
			beansInter.add(interAlbum);
		}
	    
	    
	    Article article = new Article();
	    article.getPageUtil().setPaging(true);
		article.getPageUtil().setOrderByCondition("viewTimes");
		article.getPageUtil().setQueryCondition("DESC");
		article.getPageUtil().setPageOffset(0);
		article.getPageUtil().setPageSize(6);
		article.setUseStatus('0');
		
	    List<Article> beansArticle = articleSER.selectByBean(article);
	    for (Article article2 : beansArticle) {
			InterArticle interArticle = new InterArticle();
			interArticle.setArticle(article2);
			beansInter.add(interArticle);
		}
	    
	    Banner banner = new Banner();
	    List<Banner> beansBanner = bannerSER.selectByBean(banner);
	    
	    
	    Map<String,Object> rets = new HashMap<String,Object>();
	    rets.put("header",beansBanner);
	    rets.put("body", beansInter);
	    return rets;
    	
    }
	@RequestMapping("/member")
    public @ResponseBody List<Customer> randomMember(){
       List<Customer> random = customerSER.selectByRandom();		
	   return random;
	}
	
	@RequestMapping("/detail/wen/{id}")
	public @ResponseBody Map<String,Object> detailAlbum(@PathVariable("id") Integer id) throws Exception{
		System.out.println(id);
		Album album = albumSER.selectByPrimaryKey(id);
		InterAlbum interAlbum = new InterAlbum();
		interAlbum.setAlbum(album);
		
		Chapter chapter = new Chapter();
		chapter.getPageUtil().setPaging(true);
		chapter.getPageUtil().setOrderByCondition("downloadTimes");
		chapter.getPageUtil().setQueryCondition("DESC");
		chapter.getPageUtil().setPageOffset(0);
		chapter.getPageUtil().setPageSize(6);
        chapter.getAlbum().setId(id);
        
	    List<Chapter> beansChapter = chapterSER.selectByBean(chapter);
	    
	    Map<String,Object> rets = new HashMap<String,Object>();
	    rets.put("introduction", interAlbum);
	    rets.put("list", beansChapter);
		return rets;
	}
	@RequestMapping("/detail/xiu/{id}")
	public @ResponseBody Map<String,Object> detailCounterGroup(@PathVariable ("id") Integer id) throws Exception{
	   Customer customer = new Customer();
	   customer.setId(id);
	   List<CounterGroup> list = counterGroupSER.selectByCustomer(customer);
	   Map<String,Object> map = new HashMap<String,Object>();
	   map.put("xiu", list);
	   return map;
	}
	@RequestMapping("/account/login/{name}/{password}")
	public @ResponseBody Customer login(@PathVariable("name") String name,@PathVariable("password") String password){
		Customer customer = new Customer();
		customer.setName(name);
		customer.setPassword(password);
		return customerSER.selectByCustomer(customer);
	} 
	@RequestMapping("/account/register/{phone}/{password}")
	public @ResponseBody boolean registerCustomer(@PathVariable("name") String phone,@PathVariable("password") String password) throws Exception{
		Customer customer = new Customer();
		customer.setPhone(phone);
		Customer customer2 = customerSER.selectByPrimaryKey(customer);
		if(customer2 == null){
		customer.setPassword(password);
			customer.setPhone(phone);
			customerSER.insert(customer);
		}
		return true;
	}
	@RequestMapping("/account/modify/{phone}/{password}/{id}")
	public @ResponseBody boolean modifyCustomer(@PathVariable("name") String phone,@PathVariable("password") String password,@PathVariable("id") Integer id) throws Exception{
		Customer customer = new Customer();
		customer.setPassword(password);
		customer.setPhone(phone);
		customer.setId(id);
		customerSER.updateByPrimaryKey(customer);
		return true;
	}
	@RequestMapping("/account/identify/obtain/{phone}")
	public @ResponseBody boolean obtainIdentify(@PathVariable ("phone") String phone){
		//生成验证码  发送给客户
		//设置时长 放入redis
		return true;
	}
	@RequestMapping("/account/identify/check/{code}")
	public @ResponseBody boolean checkCode(@PathVariable("code") String code){
		//验证
		return true;
	}
	@RequestMapping("/detail/si/{id}/{uid}")
	public @ResponseBody Map<String,String> findPartOfArticle(@PathVariable("id") Integer id,@PathVariable("uid") Integer Uid){
		
		return null;
	}
}
